﻿using System;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using BathroomLocator.Phone.Models;
using BathroomLocator.Phone.ViewModel;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;

namespace BathroomLocator.Phone.Pages
{
	public partial class BathroomLocationDetailsEditPage : PhoneApplicationPage
	{
		public BathroomLocationDetailsEditPage()
		{
			InitializeComponent();

			var viewModel = new BathroomLocationDetailsEditPageViewModel();
			viewModel.SaveChangesSuccess += (s, e) => this.NavigationService.GoBack();
            viewModel.DeleteSuccess += (s, e) => this.NavigationService.Navigate(new Uri("/Pages/MainPage.xaml", UriKind.Relative));

			this.DataContext = viewModel;
		}

		protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
		{
			var viewModel = this.DataContext as BathroomLocationDetailsEditPageViewModel;
			if (viewModel != null)
			{
				var editDataQueryString = this.NavigationContext.QueryString["editData"];
				bool editData;
				if (string.IsNullOrEmpty(editDataQueryString) ||
					!bool.TryParse(editDataQueryString, out editData))
				{
					editData = false;
				}

				if (viewModel.BathroomLocation == null)
				{
					viewModel.SetBathroomLocationModel(editData
						? PhoneApplicationService.Current.State["CurrentBathroomLocation"] as BathroomLocation
						: null);
				}
			}
		}

		private void OnSaveData(object sender, EventArgs e)
		{
			var viewModel = this.DataContext as BathroomLocationDetailsEditPageViewModel;
			if (viewModel != null)
			{
				var focusTextbox = FocusManager.GetFocusedElement() as TextBox;
				if (focusTextbox != null)
				{
					var binding = focusTextbox.GetBindingExpression(TextBox.TextProperty);
					binding.UpdateSource();
				}

				viewModel.SaveData();
			}
		}

		private void OnDeleteData(object sender, EventArgs e)
		{
			var viewModel = this.DataContext as BathroomLocationDetailsEditPageViewModel;
			if (viewModel != null)
			{
				viewModel.DeleteData();
			}
		}

        private void OnApplicationBarMenuItemTakePictureClick(object sender, EventArgs e)
        {
            TriggerPictureClick(sender, e);
        }

        private void ApplicationBarIconButton_Click(object sender, System.EventArgs e)
        {
        	TriggerPictureClick(sender, e);
        }
		
		private void TriggerPictureClick(object sender, System.EventArgs e)
		{
			var viewModel = this.DataContext as BathroomLocationDetailsEditPageViewModel;
            if (viewModel != null)
            {
                CameraCaptureTask task = new CameraCaptureTask();
                task.Completed += (s, result) =>
                {
                    if (result.TaskResult == TaskResult.OK)
                    {
                        var image = new BitmapImage();
                        image.SetSource(result.ChosenPhoto);

                        viewModel.SavePictureToPhone(result);
                    }
                };
                task.Show();
            }
		}
	}
}